# first use `mkdir build && cd build`
# then use `cmake .. -DUSE_COLOR_LOG=ON/OFF` to make a library
# or use `cmake .. -DUSE_COLOR_LOG=ON/OFF -GXcode` to build a project for xcode
# or use `cmake .. -DUSE_COLOR_LOG=ON/OFF -G"Visual Studio 14"` to build a project for visual studio 2015

if(WIN32)
	cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
else()
	cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR)
endif()

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE Release
		CACHE STRING "Set build type to Debug or Release (default Release)" FORCE)
endif()

string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)

set(CMAKE_CONFIGURATION_TYPES "Release;Debug" CACHE STRING "Limited configurations" FORCE)

project(libexcept C)
option(USE_COLOR_LOG "Use color logs" OFF)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckSymbolExists)
include(CheckFunctionExists)
include(AddCompilerFlags)

if(WIN32)
    set(CMAKE_EXTRA_INCLUDE_FILES winsock2.h ws2tcpip.h)
    set(CMAKE_REQUIRED_LIBRARIES  ws2_32.lib)
    set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h)
endif()

if(APPLE)
    add_compiler_flags(
        -Wno-error=deprecated-declarations
        -Qunused-arguments
    )
endif()

CHECK_SYMBOL_EXISTS(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)

if (_GNU_SOURCE)
    add_definitions(-D_GNU_SOURCE)
endif()

if (${USE_COLOR_LOG} STREQUAL "ON")
    message(STATUS "Use color log")
	set(HAVE_COLOR_LOG 1)
endif()

CHECK_INCLUDE_FILE(setjmp.h HAVE_SETJMP_H)
if(HAVE_SETJMP_H)
    list(APPEND CMAKE_EXTRA_INCLUDE_FILES setjmp.h)
else()
	message(FATAL_ERROR "not support this platform")
endif()

if (_MSC_VER)
    list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
endif()

CHECK_FUNCTION_EXISTS(siglongjmp HAVE_SIGLONGJMP)

if(HAVE_SIGLONGJMP)
    message(STATUS "support this feature that save and restore the signal mask")
endif()

set(LIB_PLATFORM)
if(UNIX)
    list(APPEND LIB_PLATFORM dl)
    list(APPEND LIB_PLATFORM m)
endif()

if(WIN32)
    set(LIB_PLATFORM ws2_32)
    add_definitions(
            -D_CRT_SECURE_NO_WARNINGS
            -D_CRT_NONSTDC_NO_DEPRECATE)
endif()

include_directories(BEFORE ${PROJECT_SOURCE_DIR})
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/include)
# ------- library start -------
set(SOURCES_DIR ${PROJECT_SOURCE_DIR}/src)
aux_source_directory(${SOURCES_DIR} LIB_SOURCES)
file(GLOB LIB_HEADERS ${SOURCES_DIR}/*.h)

source_group("HEADERS" FILES ${LIB_HEADERS})
source_group("SOURCES" FILES ${LIB_SOURCES})

# ------- library end   -------
include_directories(${SOURCES_DIR})

configure_file(
    ${PROJECT_SOURCE_DIR}/config.h.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/include/config.h
        NEWLINE_STYLE UNIX)

add_library(except STATIC
    ${LIB_HEADERS}
    ${LIB_SOURCES})


add_executable(test-except ${PROJECT_SOURCE_DIR}/test/test.c)
target_link_libraries (test-except except ${LIB_PLATFORM})

install (TARGETS except DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/lib)
install (TARGETS test-except DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin)
install (FILES ${LIB_HEADERS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)

macro (do_test name arg)
  add_test (Except${name}Test test-except ${arg})
  set_tests_properties (Except${name}Test
    PROPERTIES PASS_REGULAR_EXPRESSION ${name})
endmacro (do_test)

do_test(Normal N)
do_test(Skip S)

message(STATUS "LIB_PLATFORM:             " ${LIB_PLATFORM})
message(STATUS "CMAKE_THREAD_LIBS_INIT:   " ${CMAKE_THREAD_LIBS_INIT})

message(STATUS "CMAKE_BINARY_DIR:         " ${CMAKE_BINARY_DIR})
message(STATUS "CMAKE_CURRENT_BINARY_DIR: " ${CMAKE_CURRENT_BINARY_DIR})
message(STATUS "CMAKE_SOURCE_DIR:         " ${CMAKE_SOURCE_DIR})
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: " ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "PROJECT_BINARY_DIR:       " ${PROJECT_BINARY_DIR})
message(STATUS "PROJECT_SOURCE_DIR:       " ${PROJECT_SOURCE_DIR})
message(STATUS "CMAKE_MODULE_PATH:        " ${CMAKE_MODULE_PATH})
message(STATUS "CMAKE_COMMAND:            " ${CMAKE_COMMAND})
message(STATUS "CMAKE_ROOT:               " ${CMAKE_ROOT} )
message(STATUS "CMAKE_SYSTEM:             " ${CMAKE_SYSTEM} )
message(STATUS "CMAKE_SYSTEM_NAME:        " ${CMAKE_SYSTEM_NAME} )
message(STATUS "CMAKE_SYSTEM_VERSION:     " ${CMAKE_SYSTEM_VERSION} )
message(STATUS "CMAKE_SYSTEM_PROCESSOR:   " ${CMAKE_SYSTEM_PROCESSOR} )
message(STATUS "CMAKE_SKIP_RPATH:         " ${CMAKE_SKIP_RPATH} )
message(STATUS "CMAKE_VERBOSE_MAKEFILE:   " ${CMAKE_VERBOSE_MAKEFILE} )
message(STATUS "CMAKE_CXX_FLAGS:          " ${CMAKE_CXX_FLAGS} )
message(STATUS "CMAKE_C_FLAGS:            " ${CMAKE_C_FLAGS} )
message(STATUS "CMAKE_BUILD_TYPE:         " ${CMAKE_BUILD_TYPE} )
message(STATUS "CMAKE_CXX_COMPILER:       " ${CMAKE_CXX_COMPILER} )
message(STATUS "CMAKE_C_COMPILER:         " ${CMAKE_C_COMPILER} )
message(STATUS "CMAKE_AR:                 " ${CMAKE_AR} )
message(STATUS "CMAKE_RANLIB:             " ${CMAKE_RANLIB} )
message(STATUS "")


